//
//  MenkNavnigationBar.m
//  MenkNav
//
//  Created by Burn on 2019/6/13.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "MenkNavnigationBar.h"
#import "Macro.h"

@interface MenkNavnigationBar ()


@end

@implementation MenkNavnigationBar

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.line = [[UIView alloc]init];
        [self addSubview:self.line];
    }
    return self;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (self.backGroundImage) {
        self.backGroundImage.frame = self.bounds;
        if (![self.subviews containsObject:self.backGroundImage]) {
            self.backGroundImage.frame = self.bounds;
        }
    }
    

    self.line.frame = CGRectMake(0, self.bounds.size.height - 0.5, SCREEN_WIDTH, 0.5);

    
    if (self.rightView) {
        self.rightView.frame = CGRectMake(SCREEN_WIDTH - self.rightView.frame.size.width - 5, SafeAreaStatuBarHeight, self.rightView.frame.size.width, self.rightView.frame.size.height);
    }
    
    if (self.leftView) {
        if ([self.leftView isKindOfClass:[UIButton class]]) {
            self.leftView.frame = CGRectMake(10, (IS_IPHONE_X ? SafeAreaStatuBarHeight : 20), self.leftView.frame.size.width, self.leftView.frame.size.height);
        }else{
            self.leftView.frame = CGRectMake(10, (IS_IPHONE_X ? SafeAreaStatuBarHeight : 20) - 5, self.leftView.frame.size.width, self.leftView.frame.size.height);
        }
        DLOG(@"%@",NSStringFromCGRect(self.leftView.frame));
        if (![self.subviews containsObject:self.leftView]) {
            [self addSubview:self.leftView];
        }
    }
    
    
    if (self.midView) {
        self.midView.center = CGPointMake(self.center.x, self.frame.size.height * .5 + SafeAreaStatuBarHeight * .5);
        if (![self.subviews containsObject:self.midView]) {
            [self addSubview:self.midView];
        }
    }

    
}


- (void)setDataSource:(id<MenkNavnigationBarProtocol>)dataSource{
    
    _dataSource = dataSource;
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationHidden:)]) {
        BOOL isHidden = [self.dataSource mk_navigationHidden:self];
        if (isHidden) {
            self.hidden = YES;
        }else{
            self.hidden = NO;
        }
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationBottomLine:)]) {
        BOOL result = [self.dataSource mk_navigationBottomLine:self];
        if (result) {
            self.line.hidden = NO;
        }else{
            self.line.hidden = YES;
        }
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationBackGroundImage:)]) {
        UIImage *image = [self.dataSource mk_navigationBackGroundImage:self];
        if (image) {
            self.backGroundImage.hidden = NO;
            self.backGroundImage.image = image;
        }else{
            self.backGroundImage.hidden = YES;
        }
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationBackGroundColor:)]) {
        UIColor *color = [self.dataSource mk_navigationBackGroundColor:self];
        if (color) {
            self.backgroundColor = color;
        }else{
            self.backgroundColor = [UIColor whiteColor];
        }
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationLineColor:)]) {
        self.line.backgroundColor = [self.dataSource mk_navigationLineColor:self];
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationHeight:)]) {
        self.height = [self.dataSource mk_navigationHeight:self];
    }
    
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationBarLeft:)]) {
        self.leftView = [self.dataSource mk_navigationBarLeft:self];
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationBarMid:)]) {
        self.midView = [self.dataSource mk_navigationBarMid:self];
    }
    
    if ([self.dataSource respondsToSelector:@selector(mk_navigationBarRight:)]) {
        self.rightView = [self.dataSource mk_navigationBarRight:self];
    }
}

- (void)setBackGroundImage:(UIImageView *)backGroundImage{
    _backGroundImage = backGroundImage;
}

- (void)setHeight:(CGFloat)height{
    _height = height;
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
    if (height == 0) {
        self.hidden = YES;
    }
}

- (void)setLeftView:(UIView *)leftView{
    _leftView = leftView;
}

- (void)setMidView:(UIView *)midView{
    [_midView removeFromSuperview];
    _midView = midView;
    [self addSubview:_midView];
}

- (void)setRightView:(UIView *)rightView{
    _rightView = rightView;
    [self addSubview:_rightView];
}



@end
